package com.xlh.support.attendance.service.impl;

import com.xlh.support.attendance.po.AttendanceListQueryPO;
import com.xlh.support.attendance.service.AttendanceService;
import com.xlh.support.attendance.service.AttendanceStatisticsService;
import com.xlh.support.attendance.vo.AttendanceVO;
import com.xlh.util.ExcelsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cheer
 */
@Service
public class AttendanceStatisticsServiceImpl implements AttendanceStatisticsService {

    @Autowired
    private AttendanceService attendanceService;

    @Override
    public void download(AttendanceListQueryPO attendanceListQueryPO) {
        List<AttendanceVO> attendanceVOs = attendanceService.listAttendanceStudents(attendanceListQueryPO, true);

        List<Map<String, Object>> excelList = new ArrayList<>();
        Map<String, Object> header = new HashMap<>();
        header.put("sheetName", "考勤记录");
        excelList.add(header);

        for (AttendanceVO attendanceVO : attendanceVOs) {
            Map<String, Object> line = new HashMap<>();
            line.put("username", attendanceVO.getUsername());
            line.put("userNumber", attendanceVO.getUserNumber());
            line.put("className", attendanceVO.getClassName());
            line.put("grade", attendanceVO.getGrade());
            line.put("absentCount", attendanceVO.getAbsentCount());
            line.put("score", attendanceVO.getScore());
            excelList.add(line);
        }

        String[] columnNames = {"姓名", "学号", "班级", "年级", "旷课总次数","分数"};
        String[] keys = {"username", "userNumber", "className", "grade", "absentCount", "score"};
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        ExcelsUtil.ExcelSingleOutputStream(excelList, keys, columnNames, "attendance", request, response);
    }
}
